/**
 * 点工具
 */
import { WorldToLonLat } from './conversTool';

class DrawPoint {
  viewer: any;
  PointSource: any;

  handlerPoint: any;
  point: any;
  handler: any;
  onOff: any;
  pointList: any;
  constructor(viewer: any, PointSource:any) {
    this.viewer = viewer;
    this.PointSource = PointSource;
  }
  
  // 绘制程序的准备工作，通过其他方式触发activate
  readyDrawPointHandler() {
    this.handlerPoint = new Cesium.DrawHandler(this.viewer, Cesium.DrawMode.Point);
    this.handlerPoint.activeEvt.addEventListener((isActive) => {
      if (isActive) { // 开启绘制
        this.viewer.enableCursorStyle = false
        this.viewer._element.style.cursor = ''
      } else { // 结束绘制
        this.viewer.enableCursorStyle = true
        document.documentElement.style.cursor = ''
      }
    })
    this.handlerPoint.movingEvt.addEventListener(({ x, y }) => {
      // 绘制过程中的提示框，根据返回的屏幕坐标设置
      console.log(x,y,"绘制中");
      
    })
    this.handlerPoint.drawEvt.addEventListener((result) => {
      // 绘制结束（不是关闭绘制），确认最终点位
      this.point = result.object.position
      console.log(this.point,"绘制结束");
    })
  }

  // 开始绘制
  startDrawPoint() {
    this.handlerPoint.deactivate() // 先触发一次关闭绘制
    this.handlerPoint.clear() // 先清除一次之前的点位
    this.handlerPoint.activate() // 触发绘制
  }

  // 清除已存在的点位 会同时关闭当前存在的绘制
  clearDrawPoin() {
    this.handlerPoint.clear()
  }

  // 关闭绘制
  deactivateDrawPoin() {
    this.handlerPoint.deactivate()
  }

  // 绘制撒点
  initDrawPointEventHandler() {
    this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas) // event事件处理程序
    this.onOff = true;
    // 绑定各类事件
    this.handler.setInputAction(
      // 鼠标点击事件
      (e) => {
        if (this.onOff) {
          // 开启绘制撒点
          this.clearPointHandler(null)
          this.drawPointHandler(e.position)
        }
      },
      Cesium.ScreenSpaceEventType.LEFT_CLICK
    )
    this.handler.setInputAction(
      // 鼠标移动事件
      (e) => {
        if (this.onOff) {
          // 鼠标显示手型
          this.viewer.enableCursorStyle = false
          this.viewer._element.style.cursor = ''
          document.documentElement.style.cursor = 'pointer'
        } else {
          // 鼠标关闭手型，隐藏提示框
          this.viewer.enableCursorStyle = true
          document.documentElement.style.cursor = ''
        }
      },
      Cesium.ScreenSpaceEventType.MOUSE_MOVE
    ) // 需要shift和左键组合触发
  }

  // 绘制点位
  drawPointHandler(position:any) {
    const resultPos = this.viewer.scene.pickPosition(position)
    this.PointSource.entities.add({
      id: 'test',
      name: 'test',
      position: Cesium.Cartesian3.fromDegrees(
        ...WorldToLonLat(resultPos,this.viewer)
      ),
      point: {
        pixelSize: 10,
        color: new Cesium.Color(0, 1, 1),
      },
    })
  }
  // 清除已存在点位
  clearPointHandler(point:any) {
    // viewer.entities.removeAll() // 清除场景中所有的entity实体
    // viewer.entities.remove(point) // 传入指定的实体删除
    this.PointSource.entities.remove(this.PointSource.entities.getById('test')) // 根据id获取到对应的实体再触发删除
  }
  // 结束绘制
  endDrawPointEventHandler() {
    this.onOff = false;
    return this.pointList;
  }

  // 批量点加载
  convertPoint(pointArr:[]) {
    let pointPrimitives = this.viewer.scene.primitives.add(new Cesium.PointPrimitiveCollection());// 申明点渲染集合
    pointArr.forEach( J => {
      let position = Cesium.Cartesian3.fromDegrees(J.x, J.y, J.z);
      pointPrimitives.add({
        pixelSize: 5,
        color: Cesium.Color['BLUE'],
        outlineColor: Cesium.Color.BLACK,
        outlineWidth: 0,
        position: position,
      });
    })
  }

  /**
   *  添加点位
   * @param {名称} name
   * @param {数据} list
   */
  addBillboard(name:string, list: any) {
    list.forEach((ele) => {
      this.PointSource.entities.add({
        //经，纬，高
        position: Cesium.Cartesian3.fromDegrees(
          ele.x,
          ele.y,
          ele.z > 80 ? ele.z : 80
        ),
        id: ele.id,
        name: name,
        item: ele,
        billboard: {
          image: ele.icon,
          scale: 0.5, //放大
        },
      })
    })
  }
}

export default DrawPoint;